package com.youhong.teethcare.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.youhong.teethcare.R;
import com.youhong.teethcare.customviews.MyProgress;
import com.youhong.teethcare.customviews.RxProgressBar;
import com.youhong.teethcare.customviews.RxRoundProgress2;
import com.youhong.teethcare.customviews.TeethModelRelativeLayoutHistory;
import com.youhong.teethcare.javabeans.BrushingRecord;
import com.youhong.teethcare.services.Common;
import com.youhong.teethcare.utils.Util;

import java.util.List;

/**
 * Created by Administrator on 2018/4/3 0003.
 */
public class MyPagerAdapter extends PagerAdapter {

    List<BrushingRecord> mList;

    private LayoutInflater mLayoutInflater = null;
    Context mContext;

    public MyPagerAdapter(Context context, List<BrushingRecord> list) {
        mContext = context;
        mList = list;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        View view = mLayoutInflater.inflate(R.layout.vp_history, null, false);
        ViewHolder vh = new ViewHolder(view);
        renderData(view, position);
        container.addView(view);
        return view;

    }

    private void renderData(View v, int position) {

        ViewHolder vh = (ViewHolder) v.getTag();

        BrushingRecord record = mList.get(position);
        int totalTime = mList.get(position).getTotalTime();
        String str_totalTime = Util.concateZero(totalTime / 60) + ":" + Util.concateZero(totalTime % 60);

        //Non-RealTime type
        if (mList.get(position).getType() == 1) {

            vh.ll_realtime.setVisibility(View.GONE);
            vh.tv_nonRealTimeDuration.setText(str_totalTime);
            vh.ll_nonRealtime.setVisibility(View.VISIBLE);

        } else {
//            vh.modelRelativeLayout.areaDisplay(record.getArea1()
//                    , record.getArea2(),
//                    record.getArea3(),
//                    record.getArea4(),
//                    record.getArea5(),
//                    record.getArea6());

            vh.rxRoundProgress.setPercent(record.getPercent());
            vh.ll_nonRealtime.setVisibility(View.GONE);
            vh.tv_duration.setText(str_totalTime);

            int finishPercent = (int) ((float) record.getTotalTime() / (float) Common.userInfo.getBrushTime() * 100);
            vh.myProgress.setMax(100);
            vh.myProgress.setProgress(finishPercent);
            
            if (record.getPercent() < 60) {
                String nickname = Common.userInfo.getNickName();
                String unformat = mContext.getResources().getString(
                        R.string.history_str1);
                Spanned span = Html.fromHtml(unformat);
                vh.tv_tips.setText(span);

            } else if (record.getPercent() == 100) {

                String nickname = Common.userInfo.getNickName();
                String text = String.format(mContext.getResources().getString(R.string.history_str2), "100%");
                Spanned span = Html.fromHtml(text);
                vh.tv_tips.setText(span);

            } else {


                String nickname = Common.userInfo.getNickName();
                String unformat = mContext.getResources().getString(
                        R.string.history_str3);
                Spanned span = Html.fromHtml(unformat);
                vh.tv_tips.setText(span);
            }
        }
    }

    ImageView imageView;
    class ViewHolder {

        public ViewHolder(View v) {


            modelRelativeLayout = v.findViewById(R.id.vp_history_teethModel);
            tv_duration = v.findViewById(R.id.frag_realtimeData_history_tv_duration);
            tv_tips = v.findViewById(R.id.frag_realtimeData_history_tv_tips);
            ll_realtime = v.findViewById(R.id.vp_history_ll_realtime);
            ll_nonRealtime = v.findViewById(R.id.vp_history_ll_nonRealtime);
            tv_nonRealTimeDuration = v.findViewById(R.id.vp_history_tv_nonRealTime);
            rxRoundProgress = v.findViewById(R.id.vp_history_rxRoundProgress);
            myProgress = v.findViewById(R.id.frag_realtimeData_history_progressBar);
            v.setTag(this);

        }

        TeethModelRelativeLayoutHistory modelRelativeLayout;
        TextView tv_duration, tv_tips;
        LinearLayout ll_realtime, ll_nonRealtime;
        TextView tv_nonRealTimeDuration;
        RxRoundProgress2 rxRoundProgress;
        ProgressBar myProgress;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        //  super.destroyItem(container, position, object);
        View content_view = (View) object;
        //     mViewCache.add(content_view);
        container.removeView(content_view);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

}
